package com.prs.crm.service.crm.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.crm.CustomerAppointDao;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAppoint;
import com.prs.crm.domain.crm.Liaison;
import com.prs.crm.domain.crm.Linkman;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.CustomerAppointService;
import com.prs.crm.service.crm.LiaisonService;
import com.prs.crm.service.crm.LinkmanService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class CustomerAppointServiceImpl extends BaseServiceImpl<CustomerAppoint,CustomerAppointDao> implements CustomerAppointService{
	private PersonService personService;
	private LinkmanService linkmanService;
	private LiaisonService liaisonService;
	
	@Override
	public void saveCustomerAppoint(Customer customer, String appoints) {
		// TODO Auto-generated method stub		
		this.getDao().removeByCustomer(customer);
		Collection<Person> persons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(appoints.split(",")));
		Iterator<Person> iter = persons.iterator();
		while(iter.hasNext()) {
			CustomerAppoint appoint = new CustomerAppoint();
			appoint.setCustomer(customer);
			appoint.setPerson(iter.next());
			appoint.setRecorder(AppContext.getAccountContext().currentUser());
			appoint.setRecordTime(new Date());
			this.getDao().save(appoint);
		}
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	@Override
	public List<Person> getPersonAppoint() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Collection<Integer> findPersonIds()
	{
		return this.getDao().findPersonIds();
	}

	@Override
	public Collection<Linkman> getTodayBirthdayLinkman4Appoint(Person person) {
		// TODO Auto-generated method stub
		Collection<Linkman> results = new ArrayList<Linkman>();
		Collection<CustomerAppoint> customerAppoints = this.getDao().getCustomerAppoint(person);
		Calendar todayCalendar = Calendar.getInstance();
		
		for(CustomerAppoint appoint : customerAppoints) {
			List<Linkman> linkmans = linkmanService.getLinkmanByCustomer(appoint.getCustomer());
			
			for(Linkman linkman : linkmans) {
				if (linkman.getBirthday() != null) {
					Calendar linkmanCalendar = Calendar.getInstance();
					linkmanCalendar.setTime(linkman.getBirthday());

					if ((todayCalendar.get(Calendar.MONTH) == linkmanCalendar.get(Calendar.MONTH))
							&& (todayCalendar.get(Calendar.DAY_OF_MONTH) == linkmanCalendar.get(Calendar.DAY_OF_MONTH))) {
						results.add(linkman);
					}
				}
			}
		}
		return results;
	}

	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}

	public LinkmanService getLinkmanService() {
		return linkmanService;
	}

	@Override
	public Collection<Liaison> getTodayDueLiaison4Appoint(Person person) {
		// TODO Auto-generated method stub
		Collection<Liaison> results = new ArrayList<Liaison>();
		Collection<CustomerAppoint> customerAppoints = this.getDao().getCustomerAppoint(person);
		Calendar todayCalendar = Calendar.getInstance();
		
		for(CustomerAppoint appoint : customerAppoints) {
			Collection<Liaison> liaisons = this.getLiaisonService().getLiaison(appoint.getCustomer());
			
			for(Liaison liaison : liaisons) {
				if(liaison.getFlag() != null) {
					continue;
				}
				Calendar liaisonCalendar = Calendar.getInstance();
				if(liaison.getNextLiaison() != null) {
					liaisonCalendar.setTime(liaison.getNextLiaison());
					
					if((todayCalendar.get(Calendar.YEAR) == liaisonCalendar.get(Calendar.YEAR)) && (todayCalendar.get(Calendar.MONTH) == liaisonCalendar.get(Calendar.MONTH)) && (todayCalendar.get(Calendar.DAY_OF_MONTH) == liaisonCalendar.get(Calendar.DAY_OF_MONTH))) {
						results.add(liaison);
					}
				}
			}
		}
		return results;
	}

	public void setLiaisonService(LiaisonService liaisonService) {
		this.liaisonService = liaisonService;
	}

	public LiaisonService getLiaisonService() {
		return liaisonService;
	}

	@Override
	public Collection<Object[]> listPersonAppoint() {
		// TODO Auto-generated method stub
		return this.getDao().listPersonAppoint();
	}

	@Override
	public Collection<CustomerAppoint> getCustomerAppoint(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getCustomerAppoint(person);
	}

	
}
